<template>
  <div class="editpassword">
    <van-nav-bar
      title="修改个人信息"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
      class="vnb"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="oldm"
        name="password"
        label="原密码"
        placeholder="请输入原密码"
        :rules="[{ required: true, message: '请填写原密码' }]"
      />
      <van-field
        v-model="newm1"
        name="password2"
        label="新密码"
        placeholder="请输入新密码"
        :rules="[{ required: true, message: '请填写新密码' }]"
      />
      <van-field
        v-model="newm2"
        name="password2"
        label="确认密码"
        placeholder="请再次输入新密码"
        :rules="[{ required: true, message: '请再次填写新密码' }]"
      />

      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >保存</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { getUserAPI } from "@/api/user";
import { editUserPwdAPI } from "@/api/user";
import { Toast } from "vant";

export default {
  components: {},
  data() {
    return {
      oldm: "",
      newm1: "",
      newm2: "",
      oldPassword: "",
      password: "",
      list: {
        password: "",
      },
    };
  },
  computed: {},
  watch: {},

  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    async getuser() {
      const result = await getUserAPI();
      console.log(result);
      console.log(result.password);
      this.oldPassword = result.password;
    },
    async onSubmit() {
      // 真正代码 缺少接口就没法写

      //   console.log(this.oldPassword, this.oldm, this.newm1, this.newm2);
      //   if (this.oldPassword === this.oldm && this.newm1 === this.newm2) {
      //     const result = await editUserPwdAPI({
      //         oldPassword: this.oldm,
      //         password: this.newm2,
      //      });
      //   console.log(result);
      //   if (result.code == "success") {
      //     Toast("修改成功");
      //     this.$router.go(-1);
      //   } else {
      //       Toast("原密码错误");
      //     }
      //   } else {
      //     Toast("原密码不正确或者两次新密码不同");
      //   }

      // 假代码 缺少接口就先这样
      if (this.newm1 === this.newm2) {
        const result = await editUserPwdAPI({
          oldPassword: this.oldm,
          password: this.newm2,
        });
        console.log(result);
        if (result.code == "success") {
          Toast("修改成功");
          this.$router.go(-1);
        } else {
          Toast("原密码错误");
        }
      } else {
        Toast("原密码不正确或者两次新密码不同");
      }
    },
  },
  created() {
    this.getuser();
  },
  mounted() {},
};
</script>
<style scoped>
.vnb {
  background-color: #dbdbdb;
}
</style>